#!/bin/bash

function error()
{
    echo "postinstall error!" "$1" 
    exit
}

#
# The shell app allows the user to select the original application that will be
# updated. It then writes the name of the original app and the path of the
# app into two separate files.
#

AppName="`cat /tmp/MacSoftInstaller/_AppName.txt`"
AppPath="`cat /tmp/MacSoftInstaller/_AppPath.txt`"
GameDataName="`cat /tmp/MacSoftInstaller/_GameDataName.txt`"

echo Selected app to be updated: $AppPath$AppName

#
# On 10.3.9, the Installer doesn't seem to join resource and data forks back
# together until after the scripts are run. Thus, any resource files that we
# ditto in this script will be bad. So before doing anything, we look for all
# resource files and join them back together.
#

find "/tmp/MacSoftInstaller" -type d | while read dir ; do
	/System/Library/CoreServices/FixupResourceForks "$dir"
done

#
# Update the original application with any new files we included
#

#
# AppName is the file we want to update. However, this may not be the application's
# original name from when it was first Installed. The user may have changed the
# name of the app in the Finder (ie, "Halo.app" to "Halo Universal.app").
# To protect us from copy errors arising from file names being out of sync, the
# shell app also writes out the name of the app stored in the Package.
#

AppInPackage="`cat /tmp/MacSoftInstaller/_AppInPackage.txt`"

#
# Calculate the length of the path so we can use it to truncate the filename
#

temp="/tmp/MacSoftInstaller/$AppInPackage"
len=${#temp}

#
# Do the actual install.
# This is kinda hacky, but it's the quickest way to get this working.
# rm the old files first, and then ditto in the new files.

file1="NetSprocketCarbonLib"
file2="OpenPlayCarbonLib"
file3="Data/Language"
file4="OpenPlayLib"
file5="OpenPlay Modules"
file6="NetSyncFuncs.bundle"

# Read Me's
file7="Read Me.rtf"
file8="Read Me"
file9="Bitte lesen.rtf"
file10="Lisez-moi.rtf"

# French and German Apps
file11="Age of Empires II (FR)"
file12="Age of Empires II (DE)"
file13="Age of Empires II"

rm -v "$AppPath$AppName"
rm -v "$AppPath$file1"
rm -v "$AppPath$file2"
rm -v "$AppPath$file3"
rm -v "$AppPath$file4"
rm -v "$AppPath$file5"
rm -v "$AppPath$file6"
rm -v "$AppPath$file7"
rm -v "$AppPath$file8"
rm -v "$AppPath$file9"
rm -v "$AppPath$file10"
rm -v "$AppPath$file11"
rm -v "$AppPath$file12"
rm -v "$AppPath$file13"

#
# NetSprocketCarbonLib and OpenPlayCarbonLib just need to be removed, not replaced
#

# The old Read Me file is being replaced with a RTF, so get rid of the old and copy in the new
ditto -V --rsrc "/tmp/MacSoftInstaller/$AppInPackage" "$AppPath$AppInPackage" 
ditto -V --rsrc "/tmp/MacSoftInstaller/$file3" "$AppPath$file3"
ditto -V --rsrc "/tmp/MacSoftInstaller/$file4" "$AppPath$file4"
ditto -V --rsrc "/tmp/MacSoftInstaller/$file5" "$AppPath$file5"
ditto -V --rsrc "/tmp/MacSoftInstaller/$file6" "$AppPath$file6"
ditto -V --rsrc "/tmp/MacSoftInstaller/$file7" "$AppPath$file7"
ditto -V --rsrc "/tmp/MacSoftInstaller/$file9" "$AppPath$file9"
ditto -V --rsrc "/tmp/MacSoftInstaller/$file10" "$AppPath$file10"
ditto -V --rsrc "/tmp/MacSoftInstaller/$file11" "$AppPath$file11"
ditto -V --rsrc "/tmp/MacSoftInstaller/$file12" "$AppPath$file12"

#
# ditto is great, however it has some problems.  If you ditto a file that doesn't have
# a parent directory, ditto will create that directory for you, but will leave off
# some of the write permissions. So after we're done copying the new GameData
# files, walk through the directory structure and make sure all the dirs have
# the same permissions.
#

find "$AppPath" -type d | while read dir ; do
	chmod 775 "$dir"
done

#
# Touch the app so the Finder will show the updated version number
#

touch "$AppPath$AppName"

#
# Clean up the /tmp folder
#

rm -r "/tmp/MacSoftInstaller"/*

exit 0
